#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void matrix_multiply(double *A, double *B, double *C, int m, int n, int k) {
    // 调整循环顺序
    for (int i = 0; i < m; i++) {
        for (int l = 0; l < n; l++) {
            for (int j = 0; j < k; j++) {
                C[i * k + j] += A[i * n + l] * B[l * k + j];
            }
        }
    }
}

int main() {
    int m = 512, n = 1024, k = 2048;
    double *A = (double *)malloc(m * n * sizeof(double));
    double *B = (double *)malloc(n * k * sizeof(double));
    double *C = (double *)malloc(m * k * sizeof(double));

    srand(time(NULL));
    for (int i = 0; i < m * n; i++) A[i] = ((double)rand() / RAND_MAX);
    for (int i = 0; i < n * k; i++) B[i] = ((double)rand() / RAND_MAX);

    // 初始化 C 为零
    for (int i = 0; i < m * k; i++) C[i] = 0;

    clock_t start = clock();
    matrix_multiply(A, B, C, m, n, k);
    clock_t end = clock();

    double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
    printf("C Time: %.6f seconds\n", time_taken);

    free(A);
    free(B);
    free(C);
    return 0;
}


